package com.bignerdranch.android.ecar.Activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.bignerdranch.android.ecar.Bean.Tab;
import com.bignerdranch.android.ecar.Fragment.FoundFragment;
import com.bignerdranch.android.ecar.Fragment.HomeFragment;
import com.bignerdranch.android.ecar.Fragment.MineFragment;
import com.bignerdranch.android.ecar.R;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity{
    private FragmentTabHost mTabHost;
    private LayoutInflater mInflater;
    private List<Tab> mTabs = new ArrayList<>(3);

    //上次按下返回键的系统时间
    private long lastBackTime = 0;
    //当前按下返回键的系统时间
    private long currentBackTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mInflater = LayoutInflater.from(this);*/
        initTab();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//捕获返回键按下的事件
       /* FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.realtabcontent);
        &&fragment.getClass()==QueryFragment.class*/

        if(keyCode == KeyEvent.KEYCODE_BACK){
            //获取当前系统时间的毫秒数
            currentBackTime = System.currentTimeMillis();
            //比较上次按下返回键和当前按下返回键的时间差，如果大于2秒，则提示再按一次退出
            if(currentBackTime - lastBackTime > 2 * 1000){
                Toast.makeText(this, "再按一次返回键退出", Toast.LENGTH_SHORT).show();
                lastBackTime = currentBackTime;
            }else{ //如果两次按下的时间差小于2秒，则退出程序
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initTab() {
        Tab home = new Tab(HomeFragment.class, R.drawable.home_selector, R.string.home);
        Tab found = new Tab(FoundFragment.class, R.drawable.found_selector, R.string.found);
        Tab mine = new Tab(MineFragment.class, R.drawable.mine_selector, R.string.mine);

        mTabs.add(home);
        mTabs.add(found);
        mTabs.add(mine);

        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mInflater = LayoutInflater.from(this);
        mTabHost.getTabWidget().setDividerDrawable(R.color.white);
        //清除分割线

        for (Tab tab : mTabs) {
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(tab.getText()));
            tabSpec.setIndicator(buildIndicate(tab));
            mTabHost.addTab(tabSpec,tab.getFrag(),null);

        }
    }

    private View buildIndicate(Tab tab) {
        View view = mInflater.inflate(R.layout.item_bottom, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.icon_image);
        TextView textView = (TextView) view.findViewById(R.id.icon_indicator);
        imageView.setImageResource(tab.getImage());
        textView.setText(tab.getText());

        return view;
    }


}
